package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService{

    @Autowired
    private CustomerTransferMapper customerTransferMapper;
    @Autowired
    private CustomerMapper customerMapper;

    public List<CustomerTransfer> selectAll() {
        return customerTransferMapper.selectAll();
    }


    public PageResult query(CustomerTransferQueryObject qo) {

        int totalCount = customerTransferMapper.queryForCount(qo);
        if(totalCount == 0){
            return new PageResult();
        }
        List<CustomerTransfer> list = customerTransferMapper.queryForList(qo);
        return new PageResult(totalCount,list);
    }

    public void updateByPrimaryKey(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    public void trans(CustomerTransfer customerTransfer) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        Date transDate = new Date();
        customerTransfer.setTransUser(employee);
        customerTransfer.setTransDate(transDate);
        customerTransferMapper.insert(customerTransfer);
        customerMapper.updateChargeUser(customerTransfer.getNewSeller().getId(),customerTransfer.getCust().getId());
    }

}
